#pragma once

#include <QObject>
#include <QString>
#include <memory>
#include "kernel/domain/map_types.h"
#include "kernel/interfaces/ILogger.h"
#include "application/map/area/area.h"

namespace application {
namespace park {

/**
 * @brief 停车点类
 * @details 管理停车位的占用状态和预占用
 */
class ParkPoint : public QObject {
    Q_OBJECT

public:
    explicit ParkPoint(const QString& pointId, int floor, const kernel::Position& position,
                       std::shared_ptr<kernel::ILogger> logger = nullptr, QObject* parent = nullptr);
    ~ParkPoint() = default;

    // 基本信息
    QString getPointId() const { return m_pointId; }
    int getFloor() const { return m_floor; }
    kernel::Position getPosition() const { return m_position; }
    QString getStationName() const { return m_stationName; }
    void setStationName(const QString& stationName) { m_stationName = stationName; }

    // 占用管理
    bool isOccupied() const { return m_isOccupied; }
    int getCurrentOccupyingAgvId() const { return m_currentOccupyingAgvId; }
    bool occupy(int agvId);
    bool releaseOccupation();

    // 预占用管理
    bool isPreOccupied() const { return m_preOccupyingAgvId != -1; }
    int getPreOccupyingAgvId() const { return m_preOccupyingAgvId; }
    bool preOccupy(int agvId);
    bool releasePreOccupation(int agvId);

    // 关联区域
    void setArea(std::shared_ptr<area::Area> area);
    std::shared_ptr<area::Area> getArea() const { return m_area; }

signals:
    void occupationChanged(bool occupied, int agvId);
    void preOccupationChanged(bool preOccupied, int agvId);

private slots:
    void onAreaEntered(int agvId, const QString& areaId);
    void onAreaExited(int agvId, const QString& areaId);

private:
    QString m_pointId;
    QString m_stationName;
    int m_floor;
    kernel::Position m_position;

    bool m_isOccupied = false;
    int m_currentOccupyingAgvId = -1;
    int m_preOccupyingAgvId = -1;

    std::shared_ptr<area::Area> m_area;
    std::shared_ptr<kernel::ILogger> m_logger;
};

} // namespace park
} // namespace application
